﻿using System;
using System.Collections;
using System.Web;
using System.Web.Caching;

namespace ORM
{
    public class CacheHelper
    {
        private static readonly string _fixNameCache = "PmfORMCore_";
        /// <summary>
        /// 缓存指定的对象，并命名为key
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">要缓存的对象</param>
        internal static void SetCache(string key, object obj)
        {
            SetCache(key, obj, 30);
        }

        internal static void SetCache(string key, object obj, int iSecond)
        {
            HttpRuntime.Cache.Insert(_fixNameCache + key, obj, null, DateTime.Now.AddSeconds(Math.Abs(iSecond)), TimeSpan.Zero);   
        }

        internal static void SetCache(string key, object obj, CacheDependency cacheDependency)
        {
            HttpRuntime.Cache.Insert(_fixNameCache + key, obj, cacheDependency);   
        }
        /// <summary>
        /// 获取指定key的缓存对象
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>对象</returns>
        internal static object GetCache(string key)
        {
            return HttpRuntime.Cache.Get(_fixNameCache + key);
        }

        internal static Hashtable GetAllCacheKey()
        {
            Hashtable hash = new Hashtable();
            IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();
            while (enumerator.MoveNext())
                hash.Add(enumerator.Key.ToString(), enumerator.Value.ToString());
            return hash;
        }
    }
}
